package com.songlanyun.modules.logisticsconfig.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.saleconfig.entity.SaleConfig;
import lombok.Data;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * 商家物流配置
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-11 09:33:48
 */
@Data
@TableName("yxt_shop_logistics_config")
public class LogisticsConfig extends RootEntity<Long, LogisticsConfig> implements Serializable
{
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	@TableId
	private Long id;
	/**
	 * 所属店铺id
	 */
	private Long shopId;
	/**
	 * 是否开通自提
	 */
	private Boolean ablePickup;
	/**
	 * 是否开启系统自动备货
	 */
	private Boolean autoStock;

	/**
	 * 自动备货所需延时配置（天数最大30）
	 */
	@Min(value = 0,message = "自动备货所需天数只允许输入大于等于0的整数",groups = {AddGroup.class, UpdateGroup.class})
	@Max(value = 30,message = "自动备货所需天数请输入不大于 30 的数值",groups = {AddGroup.class,UpdateGroup.class})
	private Integer delayDays;
	/**
	 * 自动备货所需延时配置（小时数，最大23小时）
	 */
	@Min(value = 0,message = "自动备货所需小时数只允许输入大于等于0的整数",groups = {AddGroup.class,UpdateGroup.class})
	@Max(value = 23,message = "自动备货所需小时数请输入不大于 23 的数值",groups = {AddGroup.class,UpdateGroup.class})
	private Integer delayHours;
	/**
	 * 自动备货所需延时配置（分钟数，最大59分钟）
	 */
	@Min(value = 10,message = "自动备货所需分钟请输入不小于 10 的数值",groups = {AddGroup.class,UpdateGroup.class})
	@Max(value = 59,message = "自动备货所需分钟请输入不大于 59 的数值",groups = {AddGroup.class,UpdateGroup.class})
	private Integer delayMinutes;
	/**
	 * 开启提货有效期
	 */
	private Boolean ablePickupExpired;
	/**
	 *  提货过期的天数
	 */
	@Min(value = 1,message = "提货过期的天数只允许输入大于等于1的整数",groups = {AddGroup.class,UpdateGroup.class})
	@Max(value = 30,message = "提货过期的天数请输入不大30 的数值",groups = {AddGroup.class,UpdateGroup.class})
	private Integer pickupExpired;
	/**
	 * 自提过期订单处理类型（0，过期后，订单将自动完成，不退款，1，过期后，订单将自动维权，自动向买家退款）
	 */
	private OrderConstant.ExpiredOrderHandleMode expiredOrderHandleType;
	/**
	 * 运费计算策略
	 */
	private OrderConstant.FreightStrategy freightStrategy;

	@TableField(fill= FieldFill.INSERT)
	private Date createTime;
	/**
	 *
	 */
	@TableField(fill = FieldFill.INSERT_UPDATE)
	private Date updateTime;

	@Override
	public Long getIdentity() {
		return getId();
	}


	/**
	 * 获得自动备货延迟多少秒
	 * @return
	 */
	@JsonIgnore
	private long getSecond()
	{
		final int daySecond = 86400; //60 * 60 * 24
		final int hourSecond = 3600; //60 * 60
		return  this.getDelayDays() * daySecond + this.getDelayHours() * hourSecond + this.getDelayMinutes() * 60;
	}

	/**
	 * 获得自动备货时间（毫秒）
	 * @param timestamp
	 * @return
	 */
	@JsonIgnore
	public long getAutoStockUpTimestamp(long timestamp)
	{
		return timestamp + getSecond()*1000;
	}
}
